#!/bin/sh
set -e # exit on error

###
# #%L
# **********************************************************************
# ORGANIZATION  :  Pi4J
# PROJECT       :  Pi4J :: Java Examples (for BananaPro)
# FILENAME      :  build
#
# This file is part of the Pi4J project. More information about
# this project can be found here:  https://www.pi4j.com/
# **********************************************************************
# %%
# Copyright (C) 2012 - 2019 Pi4J
# %%
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Lesser Public License for more details.
#
# You should have received a copy of the GNU General Lesser Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/lgpl-3.0.html>.
# #L%
###

cd $(dirname $(readlink -f $0))

# platform constants used in this script
PLATFORM_NAME="BananaPro"
PLATFORM_ID="bananapro"

# first optional argument can be a single Java source file or "clean"
filename=$1

# if the first argument is "clean", then only clean the *.class files from the directory
if [ "$filename" = "clean" ]; then
  echo "-------------------------------------------------------------------------"
  echo " CLEANING Pi4J EXAMPLES (for $PLATFORM_NAME)"
  echo "-------------------------------------------------------------------------"
  rm -f *.class
  echo ".. done .."
  exit 0
fi

# otherwise, continue with building the samples
echo "-------------------------------------------------------------------------"
echo " BUILDING Pi4J EXAMPLES (for $PLATFORM_NAME)"
echo "-------------------------------------------------------------------------"

# check for user provided filename argument
if [ -z $filename ]; then
  # COMPILE ALL THE EXAMPLE JAVA SOURCE FILES

  # get total number of .java example source files
  total=`ls -l *.java | grep -v ^l | wc -l`
  index=1

  # iterate over all *.java files
  for filename in *.java; do
    printf "[%02d of %02d] ... compiling : $filename\n" $index $total

	# compile each *.java file with Pi4J in the classpath
	javac -classpath .:classes:/opt/pi4j/lib/'*' -d . $filename

    # increment index counter
	index=$((index+1))
  done

else
  # COMPILE SINGLE JAVA SOURCE FILE
  echo "[1 of 1] ... compiling : $filename"
  javac -classpath .:classes:/opt/pi4j/lib/'*' -d . $filename
fi

# THE FOLLOWING INSTRUCTIONS WILL EXECUTE ONE OF THE EXAMPLES
echo "-------------------------------------------------------------------------"
echo "The following command syntax can be used to execute the sample projects:"
echo "  sudo pi4j --run $PLATFORM_ID.${filename%.*} "
echo "   - or - "
echo "  sudo java -classpath .:classes:/opt/pi4j/lib/'*' $PLATFORM_ID.${filename%.*}"
echo "   - or - "
echo "  ./run ${filename%.*}"
echo "-------------------------------------------------------------------------"
